Skip to content

Fix ORCA direct dispatch for multi-value quals#1852

Open
zhangwenchao-123 wants to merge 1 commit into
apache:mainfrom
zhangwenchao-123:fix_orca_multivalue_direct_dispatch
Open

Fix ORCA direct dispatch for multi-value quals#1852
zhangwenchao-123 wants to merge 1 commit into
apache:mainfrom
zhangwenchao-123:fix_orca_multivalue_direct_dispatch

Conversation

@zhangwenchao-123

@zhangwenchao-123 zhangwenchao-123 commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

For a predicate restricting the distribution key to several constants, e.g. WHERE dist_key IN (c1, c2), the Postgres planner direct-dispatches the slice to the union of the segments the constants hash to. GPORCA gave up instead: the optimizer side already emits one datum array per value into CDXLDirectDispatchInfo, but TranslateDXLDirectDispatchInfo required every value to hash to the same segment and returned NIL otherwise, so the query was dispatched to all segments.

Collect the distinct content ids of all datum combinations instead, mirroring the planner's MergeDirectDispatchCalculationInfo behavior. A value set that spans every segment still returns NIL, since direct dispatch buys nothing there. PlanSlice->directDispatch.contentIds is already a list and the dispatcher handles multiple content ids, so no dispatcher change is needed. Add a gpdb::LAppendUniqueInt wrapper for list_append_unique_int.

On a 3-segment cluster, "select * from t1_varchar where col1_varchar in ('a','b')" now dispatches to 2 segments (PARTIAL contents) instead of all 3, matching the planner. Single-value and same-segment cases are unchanged, as is the raw-values gp_segment_id path. Update the affected expected outputs: the direct_dispatch disjunction scenarios, the bfv_dd dispatch INFO lines, and the plans in create_index, inherit, partition, partition_append, partition_prune, partition_prune_append and qp_misc_jiras whose Gather Motion drops from 3:1 to 2:1, in src/test/regress as well as in the copies of these tests carried by the pax_storage and vectorization suites.

Fixes #1839

What does this PR do?

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Breaking Changes

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:

User-facing changes:

Dependencies:

Checklist

Additional Context

CI Skip Instructions


@my-ship-it my-ship-it left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@leborchuk

Copy link
Copy Markdown
Contributor

It looks like we see some issues in tests, see:

diff -I HINT: -I CONTEXT: -I GP_IGNORE: -U3 /__w/cloudberry/cloudberry/src/test/regress/expected/inherit_optimizer.out /__w/cloudberry/cloudberry/src/test/regress/results/inherit.out
--- /__w/cloudberry/cloudberry/src/test/regress/expected/inherit_optimizer.out	2026-07-15 01:31:24.515754942 -0700
+++ /__w/cloudberry/cloudberry/src/test/regress/results/inherit.out	2026-07-15 01:31:24.599755165 -0700
@@ -2500,7 +2500,7 @@
 explain (costs off) select * from mclparted where a in(3,4,5) order by a;
 QUERY PLAN
 ___________
- Gather Motion 3:1  (slice1; segments: 3)
+ Gather Motion 2:1  (slice1; segments: 2)
    Merge Key: a
    ->  Sort
          Sort Key: a

@zhangwenchao-123

Copy link
Copy Markdown
Contributor Author

It looks like we see some issues in tests, see:

diff -I HINT: -I CONTEXT: -I GP_IGNORE: -U3 /__w/cloudberry/cloudberry/src/test/regress/expected/inherit_optimizer.out /__w/cloudberry/cloudberry/src/test/regress/results/inherit.out
--- /__w/cloudberry/cloudberry/src/test/regress/expected/inherit_optimizer.out	2026-07-15 01:31:24.515754942 -0700
+++ /__w/cloudberry/cloudberry/src/test/regress/results/inherit.out	2026-07-15 01:31:24.599755165 -0700
@@ -2500,7 +2500,7 @@
 explain (costs off) select * from mclparted where a in(3,4,5) order by a;
 QUERY PLAN
 ___________
- Gather Motion 3:1  (slice1; segments: 3)
+ Gather Motion 2:1  (slice1; segments: 2)
    Merge Key: a
    ->  Sort
          Sort Key: a

Got it, will fix.

@zhangwenchao-123
zhangwenchao-123 force-pushed the fix_orca_multivalue_direct_dispatch branch from 03b688a to 0cb2bd8 Compare July 17, 2026 06:09
For a predicate restricting the distribution key to several constants,
e.g. WHERE dist_key IN (c1, c2), the Postgres planner direct-dispatches
the slice to the union of the segments the constants hash to. GPORCA
gave up instead: the optimizer side already emits one datum array per
value into CDXLDirectDispatchInfo, but TranslateDXLDirectDispatchInfo
required every value to hash to the same segment and returned NIL
otherwise, so the query was dispatched to all segments.

Collect the distinct content ids of all datum combinations instead,
mirroring the planner's MergeDirectDispatchCalculationInfo behavior.
A value set that spans every segment still returns NIL, since direct
dispatch buys nothing there. PlanSlice->directDispatch.contentIds is
already a list and the dispatcher handles multiple content ids, so no
dispatcher change is needed. Add a gpdb::LAppendUniqueInt wrapper for
list_append_unique_int.

On a 3-segment cluster, "select * from t1_varchar where col1_varchar
in ('a','b')" now dispatches to 2 segments (PARTIAL contents) instead
of all 3, matching the planner. Single-value and same-segment cases
are unchanged, as is the raw-values gp_segment_id path. Update the
affected expected outputs: the direct_dispatch disjunction scenarios,
the bfv_dd dispatch INFO lines, and the plans in create_index,
inherit, partition, partition_append, partition_prune,
partition_prune_append and qp_misc_jiras whose Gather Motion drops
from 3:1 to 2:1, in src/test/regress as well as in the copies of
these tests carried by the pax_storage and vectorization suites.
@zhangwenchao-123
zhangwenchao-123 force-pushed the fix_orca_multivalue_direct_dispatch branch from 0cb2bd8 to 554f458 Compare July 17, 2026 09:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] ORCA misses direct dispatch for multi-value predicates (IN / OR of equalities) on the distribution key

3 participants